package com.hotel.service.impl;


import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotel.bean.ActiveUser;
import com.hotel.entity.LoginLog;
import com.hotel.mapper.LoginLogMapper;
import com.hotel.service.LoginLogService;
import com.hotel.utils.AddressUtil;
import com.hotel.utils.IPUtil;
import com.hotel.vo.LoginLogVO;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author xiaoge
 * @Date 2020/3/20 19:11
 * @Version 1.0
 **/
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper,LoginLog> implements LoginLogService {

    @Resource
    private LoginLogMapper loginLogMapper;

    /**
     * 登入日志列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public IPage<LoginLogVO> findLoginLogList(String username,String createTime,String endTime,Integer pageNum, Integer pageSize) {
        QueryWrapper<LoginLog> o = new QueryWrapper<>();
        o.orderByDesc("login_time");
        if(username!=null&&username!=""){
            o.like("username",username);
        }
        if(createTime!=null&&createTime!=""){
            o.ge("login_time",createTime);
        }
        if(endTime!=null&&endTime!=""){
            o.le("login_time",endTime+" 23:59:59");
        }
        List<LoginLog> loginLogs = loginLogMapper.selectList(o);
        List<LoginLogVO> loginLogVOS=new ArrayList<>();
        if(!CollectionUtils.isEmpty(loginLogs)){
            for (LoginLog loginLog : loginLogs) {
                LoginLogVO logVO = new LoginLogVO();
                BeanUtils.copyProperties(loginLog,logVO);
                loginLogVOS.add(logVO);
            }
        }
        List pageList = new ArrayList<>();
        int curIdx = pageNum > 1 ? (pageNum - 1) * pageSize : 0;
        for (int i = 0; i < pageSize && curIdx + i < loginLogVOS.size(); i++) {
            pageList.add(loginLogVOS.get(curIdx + i));
        }
        IPage page = new Page<>(pageNum, pageSize);
        page.setRecords(pageList);
        page.setTotal(loginLogVOS.size());
        return page;
    }

    /**
     * 删除登录日志
     *
     * @param id id
     * @return int
     */
    @Override
    public int deleteLogin(Integer id) {
        return this.baseMapper.deleteById(id);
    }

    public void deleteBatches(List<LoginLog> ids){
        for (int i = 0; i < ids.size(); i++) {
            int i1 = deleteLogin(Integer.parseInt(String.valueOf(ids.get(i).getId())));
        }
    }

    @Override
    public void clearLogin() {
        this.baseMapper.delete(null);
    }


    /**
     * 插入登入日志
     * @param request
     */
    @Transactional
    @Override
    public void add(HttpServletRequest request) {
        loginLogMapper.insert(createLoginLog(request));
    }

    /**
     * 创建登入日志
     * @param
     * @return
     */
    public static LoginLog createLoginLog(HttpServletRequest request) {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        LoginLog loginLog = new LoginLog();
        loginLog.setUsername(activeUser.getUser().getUsername());
        loginLog.setIp(IPUtil.getIpAddr(request));
        loginLog.setLocation(AddressUtil.getCityInfo(IPUtil.getIpAddr(request)));
        // 获取客户端操作系统
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        Browser browser = userAgent.getBrowser();
        OperatingSystem os = userAgent.getOperatingSystem();
        loginLog.setUserSystem(os.getName());
        loginLog.setUserBrowser(browser.getName());
        loginLog.setLoginTime(new Date());
        return loginLog;
    }



}
